﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace D3StatsClient.Commands
{
    public abstract class CommandBase : ICommand
    {
        protected Dispatcher _currentDispatcher;

        protected CommandBase()
        {
            this._currentDispatcher = Dispatcher.CurrentDispatcher;
        }

        public abstract bool CanExecute(object parameter);

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        protected void ForceInvalidateRequerySuggested()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
            (DispatcherOperationCallback)delegate(object arg)
            {
                CommandManager.InvalidateRequerySuggested();
                return null;

            }, null);
        }

        protected void InvalidateRequerySuggested()
        {
            Action action = () => CommandManager.InvalidateRequerySuggested();
            _currentDispatcher.Invoke(action);
        }

        public abstract void Execute(object parameter);
    }
}
